package com.ava.avamedia.account;

import java.util.ArrayList;

import com.ava.avamedia.R;
import com.ava.avamedia.adapter.PaymentAdapter;
import com.ava.avamedia.model.PaymentMenu;
import com.ava.avamedia.util.Constants;
import com.congnt.library.common.ConfigManager;
import com.congnt.library.utility.DialogUtils;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class PaymentActivity extends Activity {
	private ConfigManager config;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_region);
		config = new ConfigManager(this,true);
		ListView listView = (ListView) findViewById(R.id.activity_region_list_view);
		PaymentAdapter adapter = new PaymentAdapter(this, getPaymentMenu());
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				if (arg2 == 1) {
					new SendSMSAsyncTask().execute();
				}
				if (arg2 == 2) {
					Intent intent = new Intent();
					intent.setClass(PaymentActivity.this, CardPaymentActivity.class);
					intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
					startActivity(intent);
				}
				
			}

		});
	}

	public ArrayList<PaymentMenu> getPaymentMenu() {
		ArrayList<PaymentMenu> array = new ArrayList<PaymentMenu>();
		array.add(new PaymentMenu("Hướng dẫn nạp Vàng bằng thẻ cào",
				"Thêm nhiều vàng và ngày sử dụng"));
		array.add(new PaymentMenu("Nạp bằng SMS",
				"Đơn giá : 15k VNĐ = 1234 Vàng"));
		array.add(new PaymentMenu("Nạp bằng thẻ cào",
				"Đơn giá : 15K VND = 3456"));
		return array;
	}

	class SendSMSAsyncTask extends AsyncTask<Void, Void, Void> {

		private ProgressDialog dialogProgress;

		@Override
		protected Void doInBackground(Void... params) {
			SmsManager smsManager = SmsManager.getDefault();
			String phoneNumber = config.getString(Constants.SEND_SMS_TO_PHONE, "");
			smsManager.sendTextMessage("7732", null, "XoSoAndroid taikhoan", null,
					null);
			try {
				Thread.sleep(1500);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			dialogProgress = DialogUtils.showProgress(PaymentActivity.this,
					"Đang gửi tin nhắn đến tổng đài");
		}

		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			dialogProgress.dismiss();
			dialogProgress = null;
		}

	}
}
